from django import http
from django import forms
from django.conf import settings
from django.core import serializers
from django.template import loader

from EE.models import Game
from EE.models import Contract
from EE.models import AcceptedContracts
from EE.models import ReleasedContracts
from EE.models import VolatileContracts

from EE.logic import contract as contract_logic
from EE.logic import game_state as game_state_logic

from EE.views import handler

class BidForm(forms.Form):
    def __init__(self, *args, **kwargs):
      id_str = kwargs.pop('id_str', None)
      contract_type = kwargs.pop('contract_type', None)
      if (id_str == None):
        id_str = args[0]['id_str']
        contract_type = args[0]['contract_type']
      super(BidForm, self).__init__(*args, **kwargs)
      if (contract_type ==  'released'):
	    contract = ReleasedContracts.objects.filter(id=id_str).get()
      else:
	    contract = VolatileContracts.objects.filter(id=id_str).get()
      self.fields['noOfCitizens'] = forms.IntegerField(initial=contract.noOfCitizens, required=False)
      self.fields['noOfCitizens'].widget.attrs['disabled'] = 'disabled'
      self.fields['usage'] = forms.IntegerField(initial=contract.usage, required=False)
      self.fields['usage'].widget.attrs['disabled'] = 'disabled'
      self.fields['price'] = forms.IntegerField(initial=contract.price, required=False)
      self.fields['price'].widget.attrs['disabled'] = 'disabled'
      if (contract_type == 'volatile'):
	    self.fields['bid'] = forms.IntegerField(initial=contract.bid)
      else:
	    self.fields['bid'] = forms.IntegerField(initial=0)

class Bid(handler.Handler):

	def authenticated(self):
		return self.profile_type == 'Groep'
	
	def getGetForm(self):
		id_str = self.kwargs['id_str']
		contract_type = self.kwargs['type']
		return BidForm(id_str=id_str, contract_type=contract_type)
		
	def getPostForm(self):
		id_str = self.kwargs['id_str']
		contract_type = self.kwargs['type']
		self.POST['id_str'] = id_str
		self.POST['contract_type'] = contract_type
		return BidForm(self.POST)
		
	def get(self):
		context = self.getContext()
		context['contract_id'] = self.kwargs['id_str']
		context['contract_type'] = self.kwargs['type']
		content = loader.render_to_string('contract/new.html', dictionary=context)
		return http.HttpResponse(content)
		
	def post(self):
		bid = self.cleaned_data['bid']
		id_str = self.kwargs['id_str']
		contract_type = self.kwargs['type']
		state = game_state_logic.getState(self.user)
		contract, accepted = contract_logic.make_offer(id_str, contract_type, bid, state)
		if (accepted == True):
			context = self.getContext()
			content = loader.render_to_string('contract/accepted.html', dictionary=context)
			return http.HttpResponse(content)
		else:
			context = self.getContext()
			content = loader.render_to_string('contract/declined.html', dictionary=context)
			return http.HttpResponse(content)